<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 4/11/17
 * Time: 4:45 PM
 */

namespace common\models;
use common\components\big_data\bairong\Core;
use common\components\tools\Tools;
use yii\base\ErrorException;

/**
 * Class CreditOrderLog
 * @package common\models
 *
 * @property CreditOrder $creditOrder
 * @property BankBackSendLog[] $bankBackSendLogs
 */
class CreditOrderLog extends \common\models\base\CreditOrderLog
{
    /*const API_RETURN_CODE_SUCCESS = '00000';
    const API_RETURN_CODE_NO_API_BANK = '01201';

    public static function getApiReturnCodes()
    {
        return [
            self::API_RETURN_CODE_SUCCESS => '成功!',
            self::API_RETURN_CODE_NO_API_BANK => '不是api合作银行的认证!',
        ];
    }*/

    /**
     * 百融风险验证
     * @throws ErrorException
     */
    public function bigDataCheck()
    {
        $credit = $this->creditOrder;
        if ($credit->bank->sign_code!='icbc_czzh'){
            /**
             * @var self $log
             */
            $log = self::find()
                ->where(['c_name' => $credit->custname, 'c_id_card' => $credit->idno, 'c_mobile' => $credit->mobile])
                ->andWhere(['is not', 'big_data_validate_detail_swift_number', null])
                ->andWhere(['is not', 'big_data_validate_person_recognition_shift_number', null])
                ->orderBy(['id' => SORT_DESC])->one();
            if ($log&&$log->creditOrder->big_data_validate_data){
                $this->c_name = $log->c_name;
                $this->c_id_card= $log->c_id_card;
                $this->c_mobile = $log->c_mobile;
                $this->big_data_validate_detail_swift_number = $log->big_data_validate_detail_swift_number;
                $this->big_data_validate_return = $log->big_data_validate_return;
                $credit->big_data_validate_detail_shift_number = $log->creditOrder->big_data_validate_detail_shift_number;
                $credit->big_data_validate_data = $log->creditOrder->big_data_validate_person_recognition_shift_number;
                $credit->big_data_validate_data = $log->creditOrder->big_data_validate_data;
            }else{
                $log = $this;
                $log->c_name = $credit->custname;
                $log->c_id_card = $credit->idno;
                $log->c_mobile = $credit->mobile;
                $x = \Yii::$app->bairong0->check($log->c_name, $log->c_id_card, $log->c_mobile);
                $log->big_data_validate_detail_swift_number = $x['detail_list_shift_number'];
                $log->big_data_validate_person_recognition_shift_number = $x['person_recognition_shift_number'];
                $log->big_data_validate_return = serialize($x);
                $credit->big_data_validate_detail_shift_number = $x['detail_list_shift_number'];
                $credit->big_data_validate_person_recognition_shift_number = $x['person_recognition_shift_number'];
                $credit->big_data_validate_data = \Yii::$app->bairong0->formatDetailList($x['detail_list_back'])." | ".\Yii::$app->bairong0->formatPersionRecognition($x['person_recognition_back']);
            }
            if (!$log->save()){
                throw new ErrorException(Tools::makeModelGetErrorsToStringAndGetIt($log));
            }
            if (!$credit->save()){
                throw new ErrorException(Tools::makeModelGetErrorsToStringAndGetIt($credit));
            }
        }
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'credit_order_id' => '征信号',
            'time' => 'Time',
            'post_data' => 'Post Data',
            'send_return_msg' => 'Send Return Msg',
            'credit_order_data' => 'Credit Order Data',
            'old_status' => 'Old Status',
            'new_status' => 'New Status',
            'api_return_code' => 'Api Return Code',
            'api_return_msg' => 'Api Return Msg',
            'serial_number' => '序列号',
        ];
    }

	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getCreditOrder()
	{
		return $this->hasOne(CreditOrder::className(), ['id'=>'credit_order_id']);
	}
	
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getBankBackSendLogs()
	{
		return $this->hasMany(BankBackSendLog::className(), ['for_serial_number' => 'serial_number']);
	}
}